package com.uls.found.service.impl;

import java.util.List;

import com.uls.common.exception.ServiceException;
import com.uls.common.utils.DateUtils;
import com.uls.common.utils.StringUtils;
import com.uls.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uls.found.mapper.EduReportRecordLossMapper;
import com.uls.found.domain.EduReportRecordLoss;
import com.uls.found.service.IEduReportRecordLossService;

/**
 * 报失记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
public class EduReportRecordLossServiceImpl implements IEduReportRecordLossService 
{

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private EduReportRecordLossMapper eduReportRecordLossMapper;

    /**
     * 查询报失记录
     * 
     * @param lostId 报失记录主键
     * @return 报失记录
     */
    @Override
    public EduReportRecordLoss selectEduReportRecordLossByLostId(Integer lostId)
    {
        return eduReportRecordLossMapper.selectEduReportRecordLossByLostId(lostId);
    }

    /**
     * 查询报失记录列表
     * 
     * @param eduReportRecordLoss 报失记录
     * @return 报失记录
     */
    @Override
    public List<EduReportRecordLoss> selectEduReportRecordLossList(EduReportRecordLoss eduReportRecordLoss)
    {
        return eduReportRecordLossMapper.selectEduReportRecordLossList(eduReportRecordLoss);
    }

    /**
     * 新增报失记录
     * 
     * @param eduReportRecordLoss 报失记录
     * @return 结果
     */
    @Override
    public int insertEduReportRecordLoss(EduReportRecordLoss eduReportRecordLoss)
    {
        eduReportRecordLoss.setCreateTime(DateUtils.getNowDate());
        return eduReportRecordLossMapper.insertEduReportRecordLoss(eduReportRecordLoss);
    }

    /**
     * 修改报失记录
     * 
     * @param eduReportRecordLoss 报失记录
     * @return 结果
     */
    @Override
    public int updateEduReportRecordLoss(EduReportRecordLoss eduReportRecordLoss)
    {
        eduReportRecordLoss.setUpdateTime(DateUtils.getNowDate());
        return eduReportRecordLossMapper.updateEduReportRecordLoss(eduReportRecordLoss);
    }

    /**
     * 批量删除报失记录
     * 
     * @param lostIds 需要删除的报失记录主键
     * @return 结果
     */
    @Override
    public int deleteEduReportRecordLossByLostIds(Integer[] lostIds)
    {
        return eduReportRecordLossMapper.deleteEduReportRecordLossByLostIds(lostIds);
    }

    /**
     * 删除报失记录信息
     * 
     * @param lostId 报失记录主键
     * @return 结果
     */
    @Override
    public int deleteEduReportRecordLossByLostId(Integer lostId)
    {
        return eduReportRecordLossMapper.deleteEduReportRecordLossByLostId(lostId);
    }

    @Override
    public String importUser(List<EduReportRecordLoss> eduReportRecordLossList, boolean updateSupport) {

        if (StringUtils.isNull(eduReportRecordLossList) || eduReportRecordLossList.size() == 0)
        {
            throw new ServiceException("导入客户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for(EduReportRecordLoss eduReportRecordLoss:eduReportRecordLossList){
            try {

                eduReportRecordLossMapper.insertEduReportRecordLoss(eduReportRecordLoss);
                successNum++;
                successMsg.append("<br/>" + successNum + "、报失名称： " +eduReportRecordLoss.getItemName() + " 导入成功");
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、报失名称： " +eduReportRecordLoss.getItemName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
